<template>
  <div class="user">
    <el-form
      :inline="true"
      :model="formInline"
      class="demo-form-inline"
      size="small"
    >
      <el-form-item label="素材库名称">
        <el-input
          v-model="formInline.mediaCatalogName"
          placeholder="素材库名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getList()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="add('add')">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" border style="width: 100%" height="620px">
      <el-table-column prop="mediaCatalogName" label="素材库名称">
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            @click="add('edit', scope.row)"
            size="mini"
            >编 辑</el-button
          >
          <el-button
            type="danger"
            @click="del(scope.row.id, false)"
            size="mini"
            >删 除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="block pageCont">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-size="pageSizes"
        layout="total, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog
      :title="type == 'add' ? '新增' : '编辑'"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-form :model="form" ref="form" label-width="100px" class="demo-ruleForm">
        <el-form-item label="素材库名称">
          <el-input
            v-model="form.mediaCatalogName"
            placeholder="素材库名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetForm()">取 消</el-button>
        <el-button type="primary" @click="submit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { MediaCatalogList, MediaCatalogDel,MediaCatalogEdit } from "@/api/dic";
export default {
  data() {
    return {
      tableData: [],
      formInline: {},
      currentPage: 1,
      total: 0,
      pageSizes: 20,
      dialogVisible: false,
      form: {},
      type: "add",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getList();
    },
    getList() {
      MediaCatalogList({ page: this.currentPage, ...this.formInline }).then(
        (res) => {
          this.tableData = res.data.list;
          this.total = res.data.total;
        }
      );
    },
    del(id) {
      MediaCatalogDel({ id: id }).then((res) => {
        if (res.code == 0) {
          this.$message.success("删除成功");
          this.getList();
        } else {
          this.$message.error(res.data);
        }
      });
    },
    add(type,row){
        this.type=type
        this.dialogVisible = true
        if(row)
            this.form = JSON.parse(JSON.stringify(row))
    },
    resetForm() {
        this.dialogVisible = false
        this.form = {}
        this.$refs['form'].resetFields();
    },
    submit() {
        MediaCatalogEdit(this.form).then((res)=>{
            if (res.code == 0) {
                this.$message.success(this.type=='add'?"新增成功":'编辑成功');
                this.getList();
                this.resetForm()
            } else {
                this.$message.error(res.data);
            }
        })
    },
  },
};
</script>
<style lang="scss" scoped>
.user {
  ::v-deep {
    .el-image {
      width: 50px !important;
      height: 50px !important;
      .el-image__error {
        font-size: 12px;
      }
    }
  }
}
</style>